package com.cqc.jdk8.Jutil.stream.findMatch;

import com.cqc.eb.UserEB;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * 去重
 */
public class FindXTest {


    List<UserEB> userEBList = null;

    @Before
    public void before() {
        userEBList = getUserEBList();
    }



    @Test
    public void findFirst() {
        //ifPresent(System.out::println) 只对对象有效
        //userEBList.stream().filter(new Predicate<UserEB>() {
        //                             @Override
        //                             public boolean test(UserEB userEB) {
        //                                 System.out.println("+++" + userEB.toString());
        //                                 return userEB.getAge() > 20;
        //                             }
        //                         }
        //).findFirst().ifPresent(System.out::println);


        UserEB userEB = userEBList.stream()
                .filter(x -> x.getAge() > 30)
                .findFirst()
                .orElse(null);
        System.out.println(userEB);
    }

    @Test
    public void findAny() {
        //适用于并行流
        userEBList.stream()
                .filter(user->user.getAge() > 20)
                .findAny()
                .ifPresent(System.out::println);
    }



    private List<UserEB> getUserEBList() {
        userEBList = new ArrayList<>();
        userEBList.add(new UserEB("刘诗诗", 15));
        userEBList.add(new UserEB("刘亦菲", 33));
        userEBList.add(new UserEB("古力娜扎", 30));
        userEBList.add(new UserEB("陈都灵", 29));
        userEBList.add(new UserEB("杨紫", 28));
        userEBList.add(new UserEB("林青霞", 60));
        return userEBList;
    }

}
